//
//  DBManager.m
//  LoveListen
//
//  Created by 王喆 on 15/7/5.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "DBManager.h"
#import "DetailHeadModel.h"
NSString * const kLZXFavorite = @"favorites";
NSString * const kLZXDownloads = @"downloads";
NSString * const kLZXBrowses = @"browese";

@implementation DBManager
{
    FMDatabase * _database;
}
+ (DBManager *)sharedManager{
    static DBManager *manager = nil;
    @synchronized(self) {//同步 执行 防止多线程操作
        if (manager == nil) {
            manager = [[self alloc] init];
        }
    }
    return manager;


}

-(id)init{
    if (self = [super init]) {
        NSString *filePath = [self getFileFullPathWithFileName:@"app.db"];
        _database = [[FMDatabase alloc] initWithPath:filePath];
        if ([_database open]) {
            NSLog(@"数据库打开成功");
            [self creatTable];
        }else{
            NSLog(@"open failed : %@",_database.lastErrorMessage);
        }
        
    
    }
    return self;
}


-(void)creatTable{
    NSString *sql = @"create table if not exists appIn(serial integer  Primary Key Autoincrement,title Varchar(1024),appId Varchar(1024),count Varchar(1024),image1 Varchar(1024))";
    
   // title,appId,count,image1
    
    BOOL isSuccees = [_database executeUpdate:sql];
    if (!isSuccees) {
        NSLog(@"creat error : %@",_database.lastErrorMessage);
    }
    

}
- (NSString *)getFileFullPathWithFileName:(NSString *)fileName {
   NSString *docPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:docPath]) {
        return [docPath stringByAppendingFormat:@"/%@",fileName];
    }else{
        NSLog(@"Documents不存在");
        return nil;
    }
   }

-(void)insertModel:(id)model recordType:(NSString *)type{
    DetailHeadModel * appModel=  (DetailHeadModel *)model;
    if ([self isExistAppForAppId:[NSString stringWithFormat:@"%@",appModel.albumId]]) {
        return;
    }
    NSString *sql  = @"insert into appIn(title,appId,count,image1) values (?,?,?,?)";
    //BOOL *isSuccess = [_database executeUpdate:sql,appModel.title,appModel.albumId,appModel.tracks,appModel.coverLarge,type];
    NSString *albumID=[NSString stringWithFormat:@"%@",appModel.albumId];
     NSString *albumTracks=[NSString stringWithFormat:@"%@",appModel.tracks];
    
    BOOL isSuccess = [_database executeUpdate:sql,appModel.title,albumID,albumTracks,appModel.coverLarge];
    
    if (!isSuccess) {
        NSLog(@"inset error :%@",_database.lastErrorMessage);
    }
    
}

-(void)deleteModelForAppId:(NSString *)appId{
  NSString *sql = @"delete from appIn where appId = ?";
    BOOL isSuccess = [_database executeUpdate:sql,appId];
    if (!isSuccess) {
        NSLog(@"delete error%@",_database.lastErrorMessage);
    }
    
}

-(NSArray *)readModels{
 NSString *sql = @"select appId,title,count,image1 from appIn";
    FMResultSet *rs = [_database executeQuery:sql];
    NSMutableArray *arr = [NSMutableArray array];
    while ([rs next]) {
        DetailHeadModel *appModel = [[DetailHeadModel alloc]init];
        appModel.title = [rs stringForColumn:@"title"];
        appModel.albumId  = [rs stringForColumn:@"appId"];
        appModel.tracks = [rs stringForColumn:@"count"];
        appModel.coverLarge = [rs stringForColumn:@"image1"];
        
        [arr addObject:appModel];
        
        NSLog(@"********%@",appModel.coverLarge);
        
    }
    return arr;
}

-(BOOL)isExistAppForAppId:(NSString *)appId{
    NSString *sql = @"select * from appIn where appId = ?";
    FMResultSet *rs = [_database executeQuery:sql,appId];
    if ([rs next]) {//查看是否存在 下条记录 如果存在 肯定 数据库中有记录
        return YES;
    }else{
        return NO;
    }
}
-(NSInteger)getCountsFromAppWithRecordType:(NSString *)type{
 NSString *sql = @"select count(*) from appIn where recordType = ?";
    FMResultSet *rs = [_database executeQuery:sql,type];
    NSInteger count1 = 0;
    while ([rs next]) {
        //查找 指定类型的记录条数
        count1 = [[rs stringForColumnIndex:0] integerValue];
    }
    return count1;
}






@end
